import pandas as pd

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)

df_pre = pd.DataFrame()
df_after = pd.DataFrame()
list_df = []

f = open('Result_5101-20121071-25_001.csv', encoding='gbk')
str1 = f.readline()
f.close()
print(str1)
start_num = 0 if str1.startswith('设备型号') else 1
print(start_num)
# 固晶推力
# df1 = pd.read_csv('Result_5101-21010026_001.csv', encoding='gbk', header=None, usecols=[0, 1, 2])
# 焊线推力
# df1 = pd.read_csv('Result_5101-20121223-16_001.csv', encoding='gbk', header=None, usecols=[0, 1, 2])
# 焊线拉力
# df1 = pd.read_csv('Result_5101-20121107-16_001.csv', encoding='gbk', header=None, usecols=[0, 1, 2])
# 焊线推力-齐纳
df1 = pd.read_csv('Result_5101-20121142-16_001.csv', encoding='gbk', header=None, usecols=[0, 1, 2])
# 焊线拉力-齐纳
# df1 = pd.read_csv('Result_5101-20121071-25_001.csv', encoding='gbk', header=None, usecols=[0, 1, 2])

print(df1)

index1 = df1[df1[0] == 'Cpk, Cp'].index.values[0]
# todo:原有index为 index1+2
print(index1)

# print(f"上部分的划分：{index1}")
# print(f"上部分：{df1.loc[1:index1, :3]}")

df_on = df1.loc[1:index1, :3]
sql_cols = ['TestDate', 'TestTime', 'TestNo', 'Operator', 'Class', 'MachineID', 'WireDiameter', 'ChipSize', 'DB_glue',
            'Operator', 'LoadUnit', 'Sensor', 'TestMethod', 'AcceptanceVlaue', 'TestingSpeed', 'ShearHeight']

# 将上半部分的信息转换威df
for i in range(len(sql_cols)):
    df_pre.loc[0, sql_cols[i]] = df_on.iloc[i, 1]

# 固晶推力
# df_down = pd.read_csv('Result_5101-21010026_001.csv', encoding='gbk', header=None, usecols=[0, 1, 2, 3],skiprows=index1 + 2)
# 焊线推力
# df_down = pd.read_csv('Result_5101-20121223-16_001.csv', encoding='gbk', header=None, usecols=[0, 1, 2, 3],skiprows=index1 + 2)
# 焊线拉力
# df_down = pd.read_csv('Result_5101-20121107-16_001.csv', encoding='gbk', header=None, usecols=[0, 1, 2, 3],skiprows=index1 + start_num + 1)
# 焊线推力-齐纳
df_down = pd.read_csv('Result_5101-20121142-16_001.csv', encoding='gbk', header=None, usecols=[0, 1, 2, 3],skiprows=index1 + start_num + 1)
# 焊线拉力-齐纳
# df_down = pd.read_csv('Result_5101-20121071-25_001.csv', encoding='gbk', header=None, usecols=[0, 1, 2, 3],
#                       skiprows=index1 + start_num + 1)
print(df_down)
row_num = 0
rest_num =0
mark = 1
# 整除6|8|10,6为正常推拉力,8为齐纳拉力，10为齐纳推力
if len(df_down) % 8 == 0:
    for i in range(len(df_down)):
        j = i % 8
        if (j == 0 or j == 1 or j == 2 or j == 3 or j == 6 or j == 7) and df_down.loc[
            i, 2] != 'B_open_chip_to_chip':
            row_num = index1 + i + start_num + 2
            break
        elif (j == 4 or j == 5) and df_down.loc[i, 2] != 'B_open_chip_to_bonding':
            row_num = index1 + i + start_num + 2
            break
        else:
            if j == 6 or j == 7:
                df_after.loc[0, f'{"QN_open_chip_to_chip" + str(mark)}'] = round(df_down.loc[i, 3], 2)
            else:
                df_after.loc[0, f'{df_down.loc[i, 2] + str(mark)}'] = round(df_down.loc[i, 3], 2)
            if mark == 40 or i == len(df_down) - 1:
                list_df.append(pd.concat([df_pre, df_after], axis=1))
                mark = 0
            mark += 1
else:
    rest_num = len(df_down) % 8

print(list_df)
if row_num:
    print(f'{row_num}行出错！')
elif not list_df and row_num:
    if row_num > 10 - row_num:
        print(f"缺少{10 - row_num}条数据")
    else:
        print(f"多余{row_num}条数据")
